﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading;

namespace MYTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            int ret = InitComm(1);  //开串口
            if (ret != 1)
            {
               return;
            }
            Thread.Sleep(1000);      //暂停一秒钟
            timer1.Enabled = true;   //打开计时器   
        }
        [DllImport("termb.dll")]
        public static extern int InitComm(int iPortID);
        [DllImport("termb.dll")]
        public static extern int Authenticate();
        [DllImport("termb.dll")]
        public static extern int Read_Content(int Active);
        [DllImport("termb.dll")]
        public static extern int CloseComm();
        public Identity  myIdentity = new Identity();  

        public  void Display()
        {
            int i;
            byte[] buf = new byte[255];
            FileStream fs = new FileStream("wz.txt",FileMode.Open);
            fs.Read(buf,0,255);
            fs.Close();
            //读取所有的信息
            //在form 窗口中显示出来
            byte[] Name = new byte[30];
            for (  i = 0; i < 30; i++)
                Name[i] = buf[i];
            UnicodeEncoding en = new UnicodeEncoding();
            string myName = en.GetString(Name);
            textBox1.Text = en.GetString(Name );


            byte[] Sex =new byte[2];
            Sex[0] = buf[30];
            Sex[1] = buf[31];
            string mySex = en.GetString(Sex);
            textBox2.Text = en.GetString(Sex);

            byte[] Nation = new byte[4];
            for (i = 0; i < 4; i++)
                Nation[i] = buf[32 + i];
            string myNation = en.GetString(Nation);
            textBox3.Text = en.GetString(Nation);

            byte[] Birth = new byte[16];
            for (i = 0; i <16; i++)
                Birth[i] = buf[36 + i];
            string myBirth = en.GetString(Birth);
            textBox4.Text = en.GetString(Birth);

                
            byte[] Address = new byte[70];
            for (i = 0; i < 70; i++)
                Address[i] = buf[52 + i];
            string myAddress=en.GetString(Address);
            textBox5.Text = en.GetString(Address);

            byte[] ID = new byte[36];
            for (i = 0; i < 36; i++)
                ID[i] = buf[i+122];
            string myID = en.GetString(ID);
            textBox6.Text = en.GetString(ID);

            byte[] SignGov = new byte[30];
            for (i = 0; i < 30; i++)
                SignGov[i] = buf[i+158];
            string mySignGov = en.GetString(SignGov);
            textBox7.Text = en.GetString(SignGov );

            byte[] StartData = new byte[32];
            for (i = 0; i < 32; i++)
                StartData[i] = buf[i + 188];
            string myStartData = en.GetString(StartData);
            textBox8.Text = en.GetString(StartData);
            //
            List<Byte>  pic = new List<byte>(); 
            fs = new FileStream("zp.bmp", FileMode.Open);
            int len= fs.Read(buf, 0, 255);
            while (len == 255)
            {
                pic.AddRange(buf);
                len = fs.Read(buf, 0, 255);
            }
            pic.AddRange(buf.Take(len));
            fs.Close();

            pictureBox1.Image = ByteToImage(pic.ToArray());
        }
        private Image ByteToImage(byte[] btImage)
        {
            if (btImage.Length == 0)
                return null;
            System.IO.MemoryStream ms = new System.IO.MemoryStream(btImage);
            System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
            return image;
        }
         

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {
         
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            int ret = 0;
            Thread.Sleep(200);
            ret = Authenticate();      //认证
            if (ret != 1)
                {
                    return;
                }
            ret = Read_Content(1);     //读数据
            if (ret != 1)
            {
                return;
            }
            else
            {
                Display();
            }
        }
    }
    public class Identity
    {
        public string Name { get; set; }
        public string Sex { get; set; }
        public string Nation { get; set; }
        public DateTime Birth { get; set; }
        public string Address { get; set; }
        public string ID { get; set; }
        public string SignGov { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public byte[] pic{ get; set; }
    }
}
